package com.atguigu.spring.webflux.handler;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

import static org.springframework.web.reactive.function.server.ServerResponse.ok;

/**
 * @author zp
 * @version 1.0
 * @description: 基于spring webflux函数式写法
 * @date 2022/9/10 15:17
 */
@Controller
public class DemoHandler {
    public Mono<ServerResponse> hello(ServerRequest request){
        return ok().contentType(MediaType.APPLICATION_JSON)
                .body(Mono.just("hello"),String.class);
    }

    public Mono<ServerResponse> world(ServerRequest request){
        return ok().contentType(MediaType.APPLICATION_JSON)
                .body(Mono.just("world"),String.class);
    }

    public Mono<ServerResponse> times(ServerRequest request){
        //每隔一秒发送当前的时间
        return ok().contentType(MediaType.TEXT_EVENT_STREAM)
                .body(Flux.interval(Duration.ofSeconds(1))
                        .map(it -> new SimpleDateFormat("HH:mm:ss").format(new Date())),String.class);
    }
}
